﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;

namespace ConnectionButtons
{
    public partial class ConfigForm : Form
    {
        public ConfigForm(ConnectionButtonsConfig connectionButtonsConfig)
        {
            InitializeComponent();
            this.con = connectionButtonsConfig;
        }

        private ConnectionButtonsConfig con;
        private int activeCuttonIndex;

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            cmbButtons.SelectedIndex = 0;
        }

        private void chkWindowAuth_CheckedChanged(object sender, EventArgs e)
        {
            txtUserID.Enabled = !chkWindowAuth.Checked;
            txtPassword.Enabled = !chkWindowAuth.Checked;
        }

        private void cmbButtons_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetUI(activeCuttonIndex);
            SetUI(cmbButtons.SelectedIndex + 1);
            activeCuttonIndex = cmbButtons.SelectedIndex + 1;
        }

        private void SetUI(int buttonIndex)
        {
            if (buttonIndex == 1)
            {
                txtButtonName.Text = this.con.Button1Name;
                txtDataSource.Text = this.con.DataSource1;
                txtInitialCatalog.Text = this.con.InitialCatalog1;
                txtUserID.Text = this.con.UserID1;
                txtPassword.Text = this.con.Password1;
                chkWindowAuth.Checked = this.con.WindowsAuth1;
            }

            if (buttonIndex == 2)
            {
                txtButtonName.Text = this.con.Button2Name;
                txtDataSource.Text = this.con.DataSource2;
                txtInitialCatalog.Text = this.con.InitialCatalog2;
                txtUserID.Text = this.con.UserID2;
                txtPassword.Text = this.con.Password2;
                chkWindowAuth.Checked = this.con.WindowsAuth2;
            }

            if (buttonIndex == 3)
            {
                txtButtonName.Text = this.con.Button3Name;
                txtDataSource.Text = this.con.DataSource3;
                txtInitialCatalog.Text = this.con.InitialCatalog3;
                txtUserID.Text = this.con.UserID3;
                txtPassword.Text = this.con.Password3;
                chkWindowAuth.Checked = this.con.WindowsAuth3;
            }

            if (buttonIndex == 4)
            {
                txtButtonName.Text = this.con.Button4Name;
                txtDataSource.Text = this.con.DataSource4;
                txtInitialCatalog.Text = this.con.InitialCatalog4;
                txtUserID.Text = this.con.UserID4;
                txtPassword.Text = this.con.Password4;
                chkWindowAuth.Checked = this.con.WindowsAuth4;
            }

            if (buttonIndex == 5)
            {
                txtButtonName.Text = this.con.Button5Name;
                txtDataSource.Text = this.con.DataSource5;
                txtInitialCatalog.Text = this.con.InitialCatalog5;
                txtUserID.Text = this.con.UserID5;
                txtPassword.Text = this.con.Password5;
                chkWindowAuth.Checked = this.con.WindowsAuth5;
            }

            if (buttonIndex == 6)
            {
                txtButtonName.Text = this.con.Button6Name;
                txtDataSource.Text = this.con.DataSource6;
                txtInitialCatalog.Text = this.con.InitialCatalog6;
                txtUserID.Text = this.con.UserID6;
                txtPassword.Text = this.con.Password6;
                chkWindowAuth.Checked = this.con.WindowsAuth6;
            }

            if (buttonIndex == 7)
            {
                txtButtonName.Text = this.con.Button7Name;
                txtDataSource.Text = this.con.DataSource7;
                txtInitialCatalog.Text = this.con.InitialCatalog7;
                txtUserID.Text = this.con.UserID7;
                txtPassword.Text = this.con.Password7;
                chkWindowAuth.Checked = this.con.WindowsAuth7;
            }

            if (buttonIndex == 8)
            {
                txtButtonName.Text = this.con.Button8Name;
                txtDataSource.Text = this.con.DataSource8;
                txtInitialCatalog.Text = this.con.InitialCatalog8;
                txtUserID.Text = this.con.UserID8;
                txtPassword.Text = this.con.Password8;
                chkWindowAuth.Checked = this.con.WindowsAuth8;
            }

            if (buttonIndex == 9)
            {
                txtButtonName.Text = this.con.Button9Name;
                txtDataSource.Text = this.con.DataSource9;
                txtInitialCatalog.Text = this.con.InitialCatalog9;
                txtUserID.Text = this.con.UserID9;
                txtPassword.Text = this.con.Password9;
                chkWindowAuth.Checked = this.con.WindowsAuth9;
            }

            if (buttonIndex == 10)
            {
                txtButtonName.Text = this.con.Button10Name;
                txtDataSource.Text = this.con.DataSource10;
                txtInitialCatalog.Text = this.con.InitialCatalog10;
                txtUserID.Text = this.con.UserID10;
                txtPassword.Text = this.con.Password10;
                chkWindowAuth.Checked = this.con.WindowsAuth10;
            }
        }

        private void GetUI(int buttonIndex)
        {
            if (buttonIndex == 0) return;

            if (buttonIndex == 1)
            {
                this.con.Button1Name = txtButtonName.Text;
                this.con.DataSource1 = txtDataSource.Text;
                this.con.InitialCatalog1 = txtInitialCatalog.Text;
                this.con.UserID1 = txtUserID.Text;
                this.con.Password1 = txtPassword.Text;
                this.con.WindowsAuth1 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 2)
            {
                this.con.Button2Name = txtButtonName.Text;
                this.con.DataSource2 = txtDataSource.Text;
                this.con.InitialCatalog2 = txtInitialCatalog.Text;
                this.con.UserID2 = txtUserID.Text;
                this.con.Password2 = txtPassword.Text;
                this.con.WindowsAuth2 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 3)
            {
                this.con.Button3Name = txtButtonName.Text;
                this.con.DataSource3 = txtDataSource.Text;
                this.con.InitialCatalog3 = txtInitialCatalog.Text;
                this.con.UserID3 = txtUserID.Text;
                this.con.Password3 = txtPassword.Text;
                this.con.WindowsAuth3 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 4)
            {
                this.con.Button4Name = txtButtonName.Text;
                this.con.DataSource4 = txtDataSource.Text;
                this.con.InitialCatalog4 = txtInitialCatalog.Text;
                this.con.UserID4 = txtUserID.Text;
                this.con.Password4 = txtPassword.Text;
                this.con.WindowsAuth4 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 5)
            {
                this.con.Button5Name = txtButtonName.Text;
                this.con.DataSource5 = txtDataSource.Text;
                this.con.InitialCatalog5 = txtInitialCatalog.Text;
                this.con.UserID5 = txtUserID.Text;
                this.con.Password5 = txtPassword.Text;
                this.con.WindowsAuth5 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 6)
            {
                this.con.Button6Name = txtButtonName.Text;
                this.con.DataSource6 = txtDataSource.Text;
                this.con.InitialCatalog6 = txtInitialCatalog.Text;
                this.con.UserID6 = txtUserID.Text;
                this.con.Password6 = txtPassword.Text;
                this.con.WindowsAuth6 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 7)
            {
                this.con.Button7Name = txtButtonName.Text;
                this.con.DataSource7 = txtDataSource.Text;
                this.con.InitialCatalog7 = txtInitialCatalog.Text;
                this.con.UserID7 = txtUserID.Text;
                this.con.Password7 = txtPassword.Text;
                this.con.WindowsAuth7 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 8)
            {
                this.con.Button8Name = txtButtonName.Text;
                this.con.DataSource8 = txtDataSource.Text;
                this.con.InitialCatalog8 = txtInitialCatalog.Text;
                this.con.UserID8 = txtUserID.Text;
                this.con.Password8 = txtPassword.Text;
                this.con.WindowsAuth8 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 9)
            {
                this.con.Button9Name = txtButtonName.Text;
                this.con.DataSource9 = txtDataSource.Text;
                this.con.InitialCatalog9 = txtInitialCatalog.Text;
                this.con.UserID9 = txtUserID.Text;
                this.con.Password9 = txtPassword.Text;
                this.con.WindowsAuth9 = chkWindowAuth.Checked;
            }

            if (buttonIndex == 10)
            {
                this.con.Button10Name = txtButtonName.Text;
                this.con.DataSource10 = txtDataSource.Text;
                this.con.InitialCatalog10 = txtInitialCatalog.Text;
                this.con.UserID10 = txtUserID.Text;
                this.con.Password10 = txtPassword.Text;
                this.con.WindowsAuth10 = chkWindowAuth.Checked;
            }

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            GetUI(activeCuttonIndex);
            try
            {
                Stream stream = File.Open(Connect.GetConfigFile(), FileMode.OpenOrCreate);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConnectionButtonsConfig));
                xmlSerializer.Serialize(stream, this.con);
                stream.Close();
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("ConnectionButtons", ex.ToString());
                MessageBox.Show("Cannot save config !!!" + ex.Message);
            }


            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }


    }
}
